package cn.bdqn;

import cn.bdqn.pojo.mySensitiveWord.SensitiveWord;
import cn.bdqn.service.mySensitiveWord.SensitiveWordService;
import cn.bdqn.utils.RedisCache;
import cn.hutool.core.util.StrUtil;
import com.aliyuncs.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

import static cn.bdqn.utils.RedisConstants.SENSITIVE_WORD_KEY;

@Slf4j
@Component
public class MyApplicationRunner implements ApplicationRunner {

    @Autowired
    private SensitiveWordService sensitiveWordService;

    @Autowired
    private RedisCache redisCache;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<SensitiveWord> cacheList = redisCache.getCacheList(SENSITIVE_WORD_KEY);
        if (cacheList != null && cacheList.size()>0){
            log.info("-------------------执行了redis-------------------");
            return;
        }
        log.info("-----------------------执行了sql-------------------------");
        List<SensitiveWord> list = sensitiveWordService.query().list();
        if (Objects.isNull(list)){
            log.info("------------------>异常----------->"+list);
            throw new RuntimeException("系统异常");
        }
        log.info("-----------------------设置了redis-------------------------");
        redisCache.setCacheList(SENSITIVE_WORD_KEY,list);
    }
}
